<template>
  <div>
    <!-- 右边基本表格 -->
    <BasicTable @register="registerTable">
      <!-- 表格标题 -->
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ title }} </span>
      </template>

      <!-- 表格右上角自定义按钮（新增...） -->
      <template #toolbar>
        <a-button
          v-for="(item, index) in butData"
          :key="index"
          :type="item.type"
          @click="item.do"
          v-auth="item.auth"
        >
          <Icon v-if="item.icon" :icon="item.icon" /> {{ t(item.title) }}
        </a-button>
      </template>
    </BasicTable>

    <BasicModal @register="register" :title="'修改实际调试时长'" @ok="handleTsTime">
      <div style="margin-top: 20px; font-size: 15px">
        <!-- <p> 修改实际调试时长：</p> -->
        <a-input type="text" v-model:value="Times" />
      </div>
    </BasicModal>

    <BasicModal @register="registerPrice" :title="'修改单价'" @ok="handlePrice">
      <div style="margin-top: 20px; font-size: 15px">
        <!-- <p>修改工价：</p> -->
        <a-input type="text" v-model:value="Prices" />
      </div>
    </BasicModal>
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsM8MachineTaskUpdateTaskPriceList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, onMounted, ref } from 'vue';
  // import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { BasicTable, useTable, ActionItem } from '/@/components/Table';
  import { customListData } from '/@/api/test/testData';
  import { listSet } from '/@/api/test/testData';
  import { FormProps } from '/@/components/Form';
  import { schemasData, tabConfigData } from '/@/utils/custom';
  import { useModal, BasicModal } from '/@/components/Modal';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { updateTsTime, updatePrice } from '/@/api/m8/machine/updateTaskPrice/updateTsTime';
  import { handleExport } from '/@/utils/export';
  // import { isNumber } from '/@/utils/is';
  // const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('sys.role');
  //配置表单内容
  const searchForm = ref<FormProps>({
    baseColProps: { lg: 6, md: 8 }, // 表单栅格布局
    labelWidth: 90, // 表单标签宽度3
    schemas: [
      // {}
    ],
  });
  let tableColumns = ref<any>([]);
  let actionColumnData = ref<ActionItem[]>([]);
  let layVoucherView = ref<any>({});
  let listTabConfig = ref<any>({});
  const getTitle = ref({
    // 表格标题图标
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: listTabConfig.value.title,
  });
  let title = ref('');
  let butData = ref<any>([]);
  const [register, { openModal, closeModal }] = useModal();
  const [registerPrice, { openModal: openPriceModal, closeModal: closePriceModal }] = useModal();
  const { showMessage } = useMessage();
  let Times = ref('');
  let Prices = ref('');
  // var selIds = ref('');

  onMounted(async () => {
    const res = await listSet({ viewCode: 'M8_task_updateTaskPrice_list001' });
    title.value = res.layVoucherView.name;
    layVoucherView.value = res.layVoucherView;

    searchForm.value.schemas = schemasData(layVoucherView.value.listQuery.queryCols);
    tableColumns.value = layVoucherView.value.flistTabCols; //表格表头
    actionColumnData.value = layVoucherView.value.rowBtns; //表格行内按钮
    butData.value = layVoucherView.value.headBtns; //表格右上角按钮

    butData.value.forEach((item) => {
      if (item.title == '修改调试时长') {
        // console.log(item);
        auth: 'm8:machine:machine:changetime';
        item.do = () => tsTimeOpenModal();
      } else if (item.title == '修改工价') {
        auth: 'm8:machine:machine:fprice';
        item.do = () => priceOpenModal();
      }
      if (item.btnKey === 'btnExport') {
        item.do = () => btnExport();
      }
    });
    listTabConfig.value = tabConfigData(res.layVoucherView.listTabConfig);
    setProps({
      ...listTabConfig.value,
      api: customListData,
      beforeFetch: (params) => {
        params.url = listTabConfig.value.api;
        params.aaa = 'aaa';
        return params;
      },
    });
    if (!listTabConfig.value.immediate) {
      reload();
    }
  });

  const [registerTable, { reload, setProps, getSelectRowKeys, getForm }] = useTable({
    columns: tableColumns,
    formConfig: searchForm.value as FormProps,
    rowSelection: {
      type: 'checkbox',
    },
  });

  // 修改调试时长按钮
  async function tsTimeOpenModal() {
    let ids = getSelectRowKeys();
    if (ids.length) {
      openModal(true, {});
    } else {
      showMessage('请至少选择一行数据！！');
    }
  }

  // 修改工价按钮
  async function priceOpenModal() {
    let ids = getSelectRowKeys();
    if (ids.length) {
      openPriceModal(true, {});
    } else {
      showMessage('请至少选择一行数据！！');
    }
  }

  // 保存修改调试时长按钮数据
  async function handleTsTime() {
    let ids = getSelectRowKeys();
    let selIds = ids.join(',');
    let useTs = Number(Times.value);
    if (isNaN(useTs)) {
      Prices.value = '';
      showMessage('请输入数字！');
      return;
    }
    const res = await updateTsTime({ selIds, useTs: Times.value });
    showMessage(res.message);
    Times.value = '';
    reload();
    setTimeout(closeModal);
  }

  // 保存修改工价按钮数据
  async function handlePrice() {
    let ids = getSelectRowKeys();
    let selIds = ids.join(',');
    let iqty = Number(Prices.value);
    if (isNaN(iqty)) {
      Prices.value = '';
      showMessage('请输入数字！');
      return;
    }
    const res = await updatePrice({ selIds, fprice: Prices.value });
    showMessage(res.message);
    Prices.value = '';

    reload();
    setTimeout(closePriceModal);
  }

  //导出数据
  function btnExport() {
    var formdata = getForm().getFieldsValue();
    handleExport('/m8/machine/machine/exportData', formdata);
  }
</script>
<style>
  .jeesite-editable-cell__action {
    display: none;
  }
  .edit-cell-align-center {
    width: 100% !important;
  }
</style>
